<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>zabbix2.2 使用自定义脚本监控网卡流量 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>zabbix2.2 使用自定义脚本监控网卡流量</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<ol>
<li>在客户端修改配置文件  /etc/zabbix/zabbix_agentd.conf</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要改动两个地方：</p>
<ul>
<li>UnsafeUserParameters=1</li>
<li>UserParameter=my.net.if</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/usr/local/sbin/zabbix/net.sh $1 $2   //其中UserParameter用来自定义键值，（类似于net.if.in)，自己写的脚本往往会有参数，所以需要加。这是固定写法，如果脚本压根就没有什么参数，那么这个就省了。逗号后面就是我们写的脚本的路径了，再后面就是要用到的参数，有几个就写几。</p>
<ol>
<li>编写脚本</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi   /usr/<span class="built_in">local</span>/sbin/zabbix/net.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">eth=<span class="variable">$1</span></div><div class="line">io=<span class="variable">$2</span></div><div class="line">net_file=<span class="string">"/proc/net/dev"</span></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$2</span> == <span class="string">"in"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    n_new=`grep <span class="string">"<span class="variable">$eth</span>"</span> <span class="variable">$net_file</span>|awk <span class="string">'&#123;print $2&#125;'</span>`</div><div class="line">    n_old=`tail -1 /tmp/neti.log`</div><div class="line">    n=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$n_new</span>-<span class="variable">$n_old</span>"</span>|bc`</div><div class="line">    d_new=`date +%s`</div><div class="line">    d_old=`tail -2 /tmp/neti.log|head -1`</div><div class="line">    d=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$d_new</span>-<span class="variable">$d_old</span>"</span>|bc`</div><div class="line">    if_net=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$n</span>/<span class="variable">$d</span>"</span>|bc`</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$if_net</span></div><div class="line">    date +%s&gt;&gt;/tmp/neti.log</div><div class="line">    grep <span class="string">"<span class="variable">$eth</span>"</span> <span class="variable">$net_file</span>|awk <span class="string">'&#123;print $2&#125;'</span>&gt;&gt;/tmp/neti.log</div><div class="line"><span class="keyword">elif</span> [ <span class="variable">$2</span> == <span class="string">"out"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    n_new=`grep <span class="string">"<span class="variable">$eth</span>"</span> <span class="variable">$net_file</span>|awk <span class="string">'&#123;print $10&#125;'</span>`</div><div class="line">    n_old=`tail -1 /tmp/neto.log`</div><div class="line">    n=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$n_new</span>-<span class="variable">$n_old</span>"</span>|bc`</div><div class="line">    d_new=`date +%s`</div><div class="line">    d_old=`tail -2 /tmp/neto.log|head -1`</div><div class="line">    d=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$d_new</span>-<span class="variable">$d_old</span>"</span>|bc`</div><div class="line">    if_net=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$n</span>/<span class="variable">$d</span>"</span>|bc`</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$if_net</span></div><div class="line">    date +%s&gt;&gt;/tmp/neto.log</div><div class="line">    grep <span class="string">"<span class="variable">$eth</span>"</span> <span class="variable">$net_file</span>|awk <span class="string">'&#123;print $10&#125;'</span>&gt;&gt;/tmp/neto.log</div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> 0</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个脚本的思路，就是通过查看文件 /proc/net/dev  里面的数值，来计算实时网卡流量，其实我算的是一个平均值。分为进和出。如果这个脚本每隔1分钟执行一次，那么算出来的流量值就是1分钟的平均值。</p>
<ol>
<li>在脚本执行前，需要先做一个操作</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">touch /tmp/net[io].<span class="built_in">log</span></div><div class="line">date +%s &gt;&gt;/tmp/neti.log</div><div class="line">grep eth0 /proc/net/dev |awk <span class="string">'&#123;print $2&#125;'</span> &gt;&gt;/tmp/neti.log</div><div class="line">date +%s &gt;&gt;/tmp/neto.log</div><div class="line">grep eth0 /proc/net/dev |awk <span class="string">'&#123;print $10&#125;'</span> &gt;&gt;/tmp/neto.log</div><div class="line">chown zabbix  /tmp/net[io].<span class="built_in">log</span></div></pre></td></tr></table></figure>
<ol>
<li>检查脚本是否可用</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在服务端执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">zabbix_get -s 192.168.31.166 -p10050 -k <span class="string">"my.net.if[eth0,out]"</span></div></pre></td></tr></table></figure>
<ol>
<li>如果可以返回数值说明没问题了，接着在浏览器里面配置</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;组态 –&gt; 主机 –&gt; 项目  –&gt; 创建监控项</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;名称 “网卡流量出”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;类型默认“zabbix代理”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;键值  “my.net.if[eth0,out]”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据更新间隔  60</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;存档</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Zabbix/5. zabbix2.2 使用自定义脚本监控网卡流量/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
